<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * CodeIgniter User_lib Class
 *
 * Handles logic and functions for user registration and activation.
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author		Philip Sturgeon
 * @link		
 */
class User_lib
{
    var $CI;
    var $user_table = 'users';
	
	var $user_data;
	
	var $error_code = '';

    function __construct()
    {
        $this->CI =& get_instance();
        //log_message('debug', "User_lib Class Initialized");
        
		$this->CI->load->library('session');
        $this->CI->load->module_model('users', 'users_m');
        
        if( $this->logged_in() )
        {
        	$this->user_data = $this->CI->users_m->getUser( array( 'id' => $this->CI->session->userdata('user_id') ) );
        }
    }
	
	/**
	 * Set the mode of the creation
	 *
	 * @access	public
	 * @param	string
	 
	 * @return	void
	 */	
	function login($email = '', $password = '')
    {
        $this->CI->load->helper('security');
		
        if($this->CI->session->userdata('user_id')):
        	$this->error_code = 'user_already_logged_in';
            return FALSE;
        endif;
        
        // Get the user with these details
        $this->user_data = $this->CI->users_m->getUser(array('email'=>$email));
        
        // No user, or passwords do not match
        if( !$this->user_data or $this->user_data->password != dohash($password . $this->user_data->salt)):
        	$this->error_code = 'user_login_incorrect';
            return FALSE;
        endif;
        
        // Check user is active
        if ($this->user_data->is_active == 0):
            $this->error_code = 'user_inactive';
            return FALSE;
        endif;
	
		// Update last login
		$this->CI->users_m->updateLastLogin($this->user_data->id);

		// Destroy old session
		$this->CI->session->sess_destroy();
		
		// Create a fresh new session
		$this->CI->session->sess_create();
	
		// Set the user id to a CI session
		$this->CI->session->set_userdata('user_id', $this->user_data->id);
		
		// Set the language for this user
		$this->set_lang_cookie($this->user_data->lang);

		return TRUE;
	}
	
	
	function logout()
	{
		// Wipe user id
		$this->CI->session->unset_userdata('user_id');
		
		// Destroy session
        $this->CI->session->sess_destroy();
        
        // Remove the lang cookie
        $this->unset_lang_cookie();
	}
	
	function logged_in() {
		return $this->CI->session->userdata('user_id') > 0;
	}
    
	
	function check_role($role = NULL) {
		return isset($this->user_data->role) && $this->user_data->role == $role;
	}
	
	function set_lang_cookie($lang) {
		// set to zero we'll set the expiration two years from now.
		$expire = ($this->CI->config->item('sess_expiration')) ? $this->CI->config->item('sess_expiration') : (60*60*24*365*2); // CI default as correct at 1.7.0
		
		// Set the lang setting in a native PHP cookie so it can be picked up by hooks/pick_language.php
		setcookie(
			'lang_code',
			$lang,
			$expire + time(),
			$this->CI->config->item('cookie_path'),
			$this->CI->config->item('cookie_domain'),
			0
		);
	}
	
	function unset_lang_cookie() {
		
		$_SESSION['lang_code'] = NULL;
		
		// Unset the lang setting
		setcookie(
			'lang_code',
			'',
			0,
			$this->CI->config->item('cookie_path'),
			$this->CI->config->item('cookie_domain'),
			0
		);
	}
}
?>